package com.travelmood.service;

import com.travelmood.mapper.DiaryMapper;
import com.travelmood.pojo.Diary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
@Slf4j//调试用的
@Service
public class DiaryService {
    @Autowired
    private DiaryMapper diaryMapper;

    public void addDiary(Diary diary,Integer id,Integer placeId) {
        diary.setUserId(id);
        diaryMapper.insertdiary(diary);
         log.info("Diary inserted with ID: {}", diary.getDiaryId());  // 输出日记的 ID
         Map<String, Object> params = new HashMap<>();
         params.put("diaryId", diary.getDiaryId());  // 获取自动生成的 diaryId
         params.put("placeId", placeId);
         diaryMapper.insertDiaryPlace(params);
         log.info("DiaryPlace inserted with diaryId: {} and placeId: {}", diary.getDiaryId(), placeId);
        }
    //同时在diary和diary-place表中插入diary 否则无法进行后续的查询操作

    public List<Diary> getDiarysByUserId(Integer id) {
        return diaryMapper.getdiarysByUserId(id);
    }

    public void updatediary(Diary diary, Integer id) {
        diary.setUserId(id);
        diaryMapper.updatediary(diary);
    }

    public void deleteDiaryById(Integer id, Integer userId) {
        diaryMapper.deleteDiaryById(id);
    }

    public Diary findDiaryById(Integer id) {
        return diaryMapper.findDiaryById(id);
    }

    public Integer getPlaceIdById(Integer diaryId) {
        return diaryMapper.getPlaceIdById(diaryId);
    }

    public void deleteDiaryPlaceById(Integer diaryId) {
        diaryMapper.deleteDiaryById(diaryId);
    }
}
